package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.*;
import com.x.erp.rpc.*;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.*;

/**
 * 多级营销活动信息
 */
@RestController
@RequestMapping("/multilevelActivity")
public class MultilevelActivityController extends BaseController{

	@Reference
	private MultilevelActivityRpcService multilevelActivityRpcService;// 多级营销活动信息服务

	@Reference
	private MultilevelActivityBondRpcService multilevelActivityBondRpcService;// 多级营销活动保证金服务

	@Reference
	private MultilevelActivityBuyRpcService multilevelActivityBuyRpcService;// 多级营销车辆购买服务

	@Reference
	private MultilevelActivityShareRpcService multilevelActivityShareRpcService;// 多级营销分享服务

	@Reference
	private MultilevelActivityRewardRpcService multilevelActivityRewardRpcService;// 多级营销奖励级别服务

	/**
	 * 查询多级营销活动信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityList(MultilevelActivity multilevelActivity) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivityRpcService.selectMultilevelActivityList(multilevelActivity));
	}

	/**
	 * 查询多级营销活动信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityNum(MultilevelActivity multilevelActivity) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", multilevelActivityRpcService.selectMultilevelActivityNum(multilevelActivity));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询多级营销活动信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivity", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivity(MultilevelActivity multilevelActivity) {
		if(multilevelActivity == null || StringUtils.isEmpty(multilevelActivity.getActivityId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "ID不能为空！");
		}

		//查询该活动ID是否有成交记录，如果无直接return
		MultilevelActivityBuy multilevelActivityBuy = new MultilevelActivityBuy();
		multilevelActivityBuy.setActivityId(multilevelActivity.getActivityId());
		List<MultilevelActivityBuy> multilevelActivityBuyList = multilevelActivityBuyRpcService.selectMultilevelActivityBuyList(multilevelActivityBuy);
		if (multilevelActivityBuyList != null && multilevelActivityBuyList.size() > 0){
			if (multilevelActivityBuyList.get(0).getBuyState() == 2){
				multilevelActivityBuy = multilevelActivityBuyList.get(0);
			}
		}
		multilevelActivity.setPageIndex(1);
		multilevelActivity.setPageSize(1);
		List<MultilevelActivity> multilevelActivitys = multilevelActivityRpcService.selectMultilevelActivityList(multilevelActivity);
		if(multilevelActivitys != null && multilevelActivitys.size() > 0) {
			multilevelActivity = multilevelActivitys.get(0);
			multilevelActivity.setMultilevelActivityBuy(multilevelActivityBuy);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", multilevelActivity);
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增多级营销活动信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertMultilevelActivity", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertMultilevelActivity(MultilevelActivity multilevelActivity) {
		if(multilevelActivity == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销活动信息不能为空！");
		}
		if(multilevelActivity.getActivityLevel() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "活动级别不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivity.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivity.getShopId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车商ID不能为空！");
		}
		if(StringUtils.isEmpty(multilevelActivity.getUserId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		if(multilevelActivity.getActivityBounty() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "活动赏金不能为空！");
		}
		if(multilevelActivity.getActivityDuration() == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "有效期不能为空！");
		}
		MultilevelActivityBond bond = new MultilevelActivityBond();
		bond.setShopId(multilevelActivity.getShopId());
		List<MultilevelActivityBond> multilevelActivityBonds = multilevelActivityBondRpcService.selectMultilevelActivityBondList(bond);
		if(multilevelActivityBonds.isEmpty()){
			return new ResultModel(ResultCode.RESULT_FAIL, "未缴纳保证金，不能参与该活动，请联系管理员！");
		}
		bond = multilevelActivityBonds.get(0);
		if(bond.getBondMoney().compareTo(multilevelActivity.getActivityBounty()) == -1){
			return new ResultModel(ResultCode.RESULT_FAIL, "保证金不足，不能参与该活动！");
		}
		Date cDate = this.getDate(0);
		multilevelActivity.setActivityState(1);
		multilevelActivity.setActivityEndTime(this.getDate(cDate, multilevelActivity.getActivityDuration()));
		multilevelActivity.setActivityPublishTime(cDate);
		multilevelActivity.setActivityCreateTime(cDate);
		multilevelActivity.setActivityUpdateTime(cDate);
		multilevelActivity = multilevelActivityRpcService.insertMultilevelActivity(multilevelActivity);
		if(multilevelActivity == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "不能参与该活动，请联系管理员！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", multilevelActivity);
	}

	/**
	 * 修改多级营销活动信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/updateMultilevelActivity", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateMultilevelActivity(MultilevelActivity multilevelActivity, String[] versionList) {
		if(multilevelActivity == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "多级营销活动信息不能为空！");
		}
		multilevelActivityRpcService.updateMultilevelActivity(multilevelActivity);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	/**
	 * 销售商机成交链路
	 * @param activityId
	 * @return
	 */
	@RequestMapping(value = "/getMultilevelActivityDealLink", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getMultilevelActivityDealLink(String activityId){
		if(StringUtils.isEmpty(activityId)){
			return new ResultModel(ResultCode.RESULT_FAIL, "活动ID不能为空！");
		}
		//查询该活动ID是否有成交记录，如果无直接return
		MultilevelActivityBuy multilevelActivityBuy = new MultilevelActivityBuy();
		multilevelActivityBuy.setActivityId(activityId);
		List<MultilevelActivityBuy> multilevelActivityBuyList = multilevelActivityBuyRpcService.selectMultilevelActivityBuyList(multilevelActivityBuy);
		if (multilevelActivityBuyList != null && multilevelActivityBuyList.size() > 0){
			if (multilevelActivityBuyList.get(0).getBuyState() == 1){
				return new ResultModel(ResultCode.RESULT_FAIL, "该分享无成交记录！");
			}
		}
		//查询活动级别和活动赏金
		MultilevelActivity multilevelActivity = new MultilevelActivity();
		multilevelActivity.setActivityId(activityId);
		List<MultilevelActivity> multilevelActivityList = multilevelActivityRpcService.selectMultilevelActivityList(multilevelActivity);
		Integer level = null;
		BigDecimal activityBounty = null;
		if (multilevelActivityList != null && multilevelActivityList.size() > 0){
			level = multilevelActivityList.get(0).getActivityLevel();
			activityBounty = multilevelActivityList.get(0).getActivityBounty();
		}
		//查询奖励级别
		MultilevelActivityReward multilevelActivityReward = new MultilevelActivityReward();
		multilevelActivityReward.setRewardTotalLevel(level);
		List<MultilevelActivityReward> multilevelActivityRewardList = multilevelActivityRewardRpcService.selectMultilevelActivityRewardList(multilevelActivityReward);
		//循环活动级别
		String shareParentId = null;
		MultilevelActivityShare multilevelActivityShare = new MultilevelActivityShare();
		shareParentId = multilevelActivityBuyList.get(0).getShareId();
		//封装返回结果
		List<MultilevelActivityLinkResult> multilevelActivityLinkResults = new ArrayList<>();
		BigDecimal count = new BigDecimal(0);
		for (int j = 0; j < level; j++){
			MultilevelActivityLinkResult multilevelActivityLinkResult = new MultilevelActivityLinkResult();
			if (multilevelActivityRewardList != null && multilevelActivityRewardList.size() > 0){
				MultilevelActivityReward multilevelActivityRewardInfo = multilevelActivityRewardList.get(j);
				//如果有查询分享记录，找出成交链路
				multilevelActivityShare.setShareId(shareParentId);
				List<MultilevelActivityShare> multilevelActivityShareList = multilevelActivityShareRpcService.selectMultilevelActivityShareList(multilevelActivityShare);
				if (multilevelActivityShareList != null && multilevelActivityShareList.size() > 0){
					shareParentId = multilevelActivityShareList.get(0).getShareParentId();
					multilevelActivityLinkResult.setUserNickname(multilevelActivityShareList.get(0).getUserNickname());
					multilevelActivityLinkResult.setUserMobile(multilevelActivityShareList.get(0).getUserMobile());
					multilevelActivityLinkResult.setUserLevel(j+1);
					multilevelActivityLinkResult.setActivityBounty(activityBounty);
					multilevelActivityLinkResult.setUserId(multilevelActivityShareList.get(0).getUserId());
					if (StringUtils.isNotEmpty(shareParentId)){
						//如果不是最后一级，奖励占比是正常的
						multilevelActivityLinkResult.setRewardRatio(multilevelActivityRewardInfo.getRewardRatio());
						multilevelActivityLinkResults.add(multilevelActivityLinkResult);
						BigDecimal rewardRatio = multilevelActivityRewardInfo.getRewardRatio();
						count = count.add(rewardRatio);
					}else{
						//如果是最后一级，则用1减去前面的
						multilevelActivityLinkResult.setRewardRatio(new BigDecimal(1).subtract(count));
						multilevelActivityLinkResults.add(multilevelActivityLinkResult);
						break;
					}
				}
			}
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功！", multilevelActivityLinkResults);
	}

}
